#include "qmemerydataeditor.h"
#include "ui_qmemerydataeditor.h"

#include "../../libs/sharedLib/qcommonstruct.h"
#include "../../libs/sharedLib/qabstractproperty.h"
#include "../../libs/sharedLib/qprojectcore.h"
#include "../../libs/sharedLib/qmemerymanager.h"
#include "../../libs/sharedLib/qabstracthost.h"

#include <QApplication>

#include <QComboBox>
#include <QLineEdit>
#include <QPainter>
#include <QStyleOptionViewItemV3>
#include <QStyle>
#include <QApplication>
#include <QTreeWidgetItem>
#include <QHeaderView>

QWidget *QMemeryDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
{
    QWidget *ret=NULL;
    if(index.column()==0)
    {
        QStringList list=m_editor->m_property->object()->property("memeryList").toStringList();
        QList<QString>   items;
        SHAREDLIB::QAbstractHost* host=(SHAREDLIB::QAbstractHost*)m_editor->m_property->object()->property("host").toInt();
        if(host!=NULL)
        {
            SHAREDLIB::QAbstractProperty *pro;
            foreach(const QString& name,list)
            {
                pro=host->getProperty(name);
                if(pro!=NULL)
                {
                    items.append(pro->attribute("property_name").toString());
                }
            }
            if(items.size()>0)
            {
                QComboBox *c=new QComboBox(parent);
                c->addItems(items);
                c->setCurrentIndex(c->findText(index.data().toString()));
                ret=c;
            }
        }

    }
    else if(index.column()==1)
    {
        QComboBox *c=new QComboBox(parent);
        c->addItems(SHAREDLIB::QProjectCore::getCore()->memeryManager()->groups());
        c->setCurrentIndex(c->findText(index.data().toString()));
        ret=c;
    }
    else if(index.column()==2)
    {
        QString group=index.model()->index(index.row(),1,index.parent()).data().toString();
        QComboBox *com=new QComboBox(parent);
        QStringList list=SHAREDLIB::QProjectCore::getCore()->memeryManager()->namesList(group);
        com->addItems(list);
        com->setCurrentIndex(com->findText(index.data().toString()));
        ret=com;
    }
    else if(index.column()==3)
    {
        QLineEdit *e=new QLineEdit(parent);
        e->setText(index.data().toString());
    }
    if(ret!=NULL)
    {
        ret->setProperty("no-ManhattanStyle",true);
    }
    return ret;
}

void QMemeryDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect.adjusted(0,0,0,-1));
}

void QMemeryDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{

}

QSize QMemeryDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size=QItemDelegate::sizeHint(option,index);
    size.setHeight(21);
    return size;
}

void QMemeryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(editor->inherits("QComboBox"))
    {
        QComboBox *b=(QComboBox*)editor;
        model->setData(index,b->currentText());
        if(index.column()==0)
        {
            SHAREDLIB::QAbstractHost* host=(SHAREDLIB::QAbstractHost*)m_editor->m_property->object()->property("host").toInt();
            if(host!=NULL)
            {
                QString text=b->currentText();
                QList<SHAREDLIB::QAbstractProperty*> pros=host->propertys();
                foreach(SHAREDLIB::QAbstractProperty* pro,pros)
                {
                    if(pro->attribute("property_name").toString()==text)
                    {
                        model->setData(index,pro->attribute("name"),Qt::UserRole);
                        break;
                    }
                }
            }
        }
    }
    else
    {
        QItemDelegate::setModelData(editor,model,index);
    }
}

void QMemeryDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=option;

    QColor c=QColor(255,255,191);

    if(opt.features & QStyleOptionViewItemV2::Alternate)
    {
        c=c.lighter(112);
    }
    painter->fillRect(option.rect,c);
    opt.state &=~ QStyle::State_HasFocus;
    QItemDelegate::paint(painter,opt,index);
    opt.palette.setCurrentColorGroup(QPalette::Active);
    QColor color=static_cast<QRgb>(qApp->style()->styleHint(QStyle::SH_Table_GridLineColor,&opt));
    painter->setPen(color);

    int right=option.direction==Qt::LeftToRight?option.rect.right():option.rect.left();
    painter->drawLine(right,option.rect.y(),right,option.rect.bottom());

    painter->drawLine(option.rect.left(),option.rect.bottom(),option.rect.right(),option.rect.bottom());
}

QMemeryDataEditor::QMemeryDataEditor(SHAREDLIB::QAbstractProperty *property):
    QDialog((QWidget*)qApp->property("global_widget").toInt()),
    m_property(property),
    ui(new Ui::QMemeryDataEditor)
{
    this->setStyle(QApplication::style());
    setProperty("panelwidget",true);
    ui->setupUi(this);

    ui->dataList->setItemDelegate(new QMemeryDelegate(this));
    ui->dataList->setAlternatingRowColors(true);
    ui->dataList->setEditTriggers(QAbstractItemView::EditKeyPressed);
    ui->dataList->setRootIsDecorated(false);
    ui->dataList->header()->setMovable(false);

    MemeryValueItems m=m_property->getValue().value<MemeryValueItems>();
    SHAREDLIB::QAbstractProperty *pro;
    SHAREDLIB::QAbstractHost* host=(SHAREDLIB::QAbstractHost*)m_property->object()->property("host").toInt();
    if(host==NULL)
    {
        return;
    }
    QTreeWidgetItem *item;
    foreach(tagMemeryValueInfo info,m)
    {
        pro=host->getProperty(info.m_propertyName);
        if(pro==NULL)
        {
            continue;
        }
        item=new QTreeWidgetItem(ui->dataList);
        item->setText(0,pro->attribute("property_name").toString());
        item->setData(0,Qt::UserRole,pro->attribute("name").toString());
        item->setText(1,info.m_valueGroup);
        item->setText(2,info.m_valueName);
        item->setText(3,info.m_script);
        item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        m_items.append(item);
    }

    connect(ui->dataList,SIGNAL(clicked(QModelIndex)),ui->dataList,SLOT(edit(QModelIndex)));
}

QMemeryDataEditor::~QMemeryDataEditor()
{
    delete ui;
}

void QMemeryDataEditor::on_cancelBtn_clicked()
{
    close();
}

void QMemeryDataEditor::on_addBtn_clicked()
{
    QTreeWidgetItem *item;
    item=new QTreeWidgetItem(ui->dataList);
    item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    m_items.append(item);
}

void QMemeryDataEditor::on_delBtn_clicked()
{
    foreach(QTreeWidgetItem *item,m_items)
    {
        if(item->isSelected())
        {
            m_items.removeAll(item);
            delete item;
            return;
        }
    }
}

void QMemeryDataEditor::on_okBtn_clicked()
{
    MemeryValueItems m;
    tagMemeryValueInfo info;
    foreach(QTreeWidgetItem *item,m_items)
    {
        info.m_propertyName=item->data(0,Qt::UserRole).toString();
        info.m_valueName=item->text(2);
        info.m_valueGroup=item->text(1);
        info.m_script=item->text(3);
        if(info.m_propertyName==""
                || info.m_valueName==""
                || info.m_valueGroup=="")
        {
            return;
        }
        m.append(info);
    }
    QVariant v;
    v.setValue<MemeryValueItems>(m);
    m_property->setSignalValue(v);
    close();
}
